package user

import (
	"Project/global"
	"Project/model/common"
	"Project/model/user"
	"Project/model/user/request"
	"Project/model/user/response"
	"Project/utils"
	"errors"
	"go.uber.org/zap"
	"log"
)

type UserSuperService struct{}

func (u *UserSuperService) CreateUser(userInfo request.UserInfo) (err error) {
	/**
	根据auth判断用户创建的角色
	生成用户id
	插入记录
	*/
	// 单个角色
	if len(userInfo.Auth) == 1 {
		err = u.SingleUser(userInfo)

	} else if len(userInfo.Auth) == 2 {
		err = u.MoreUsers(userInfo)
	}
	if err != nil {
		return err
	}
	return nil
}

// SuperInfo 获取超级管理员信息
func (u *UserSuperService) SuperInfo(userID int64) (super response.SuperInfo, err error) {
	info, err := u.SuperInfoByUserID(userID)
	if err != nil {
		return super, err
	}
	super = response.SuperInfo{
		UserName: info.UserName,
		Card:     info.Card,
		Phone:    info.Phone,
	}
	return super, nil
}

// EditorPassword 修改超级管理员密码
func (u *UserSuperService) EditorPassword(userID int64, pwdInfo request.SuperPwd) (err error) {
	//  校验原始密码
	super, err := u.SuperInfoByUserID(userID)
	if err != nil {
		return errors.New("用户不存在")
	}
	if !utils.BcryptCheck(pwdInfo.OldPwd, super.Password) {
		return errors.New("原始密码不正确")
	}
	if pwdInfo.OldPwd == pwdInfo.NewPwd {
		return errors.New("新旧密码相同,请重新修改")
	}
	if pwdInfo.NewPwd != pwdInfo.ConfirmPwd {
		return errors.New("两次输入密码不同")
	}
	if err = global.GVA_DB.Model(&user.UserSuper{}).Where("super_id = ?", userID).Update("password", utils.BcryptHash(pwdInfo.NewPwd)).Error; err != nil {
		return err
	}
	return nil
}

// EditorSuperInfo 修改管理员个人信息
func (u *UserSuperService) EditorSuperInfo(userID int64, userInfo request.SuperInfo) (err error) {
	ur, err := u.SuperInfoByUserID(userID)
	if err != nil {
		log.Println("获取用户信息失败", err)
		global.GVA_LOG.Error("获取用户信息失败", zap.Error(err))
		return
	}
	if ur.UserName == userInfo.UserName && ur.Card == userInfo.Card && ur.Phone == userInfo.Phone {
		return
	}
	if err = global.GVA_DB.Model(&user.UserSuper{}).Where("super_id = ?", userID).Updates(map[string]interface{}{"user_name": userInfo.UserName, "phone": userInfo.Phone, "card": userInfo.Card}).Error; err != nil {
		log.Println("更新用户信息失败", err)
		return
	}
	return
}

// SuperInfoByUserID 获取超级管理员个人信息
func (u *UserSuperService) SuperInfoByUserID(userID int64) (super user.UserSuper, err error) {
	if err = global.GVA_DB.Find(&super, "super_id = ?", userID).Error; err != nil {
		return super, err
	}
	return super, nil
}

// DelAreaUserInfo 删除区县用户
func (u *UserSuperService) DelAreaUserInfo(userID int64) (err error) {
	if err = global.GVA_DB.Where("user_id = ?", userID).Delete(&user.UserArea{}).Error; err != nil {
		return err
	}
	return nil
}

// EditorAreaUserInfo 修改区县用户信息
func (u *UserSuperService) EditorAreaUserInfo(info request.AreaInfo) (err error) {
	ur := user.UserArea{}
	if err = global.GVA_DB.Find(&ur, "user_id = ?", info.UserID).Error; err != nil {
		log.Println("查询用户信息失败")
		return
	}

	if ur.UserName == info.UserName && ur.Card == info.Card && ur.Phone == info.Phone && ur.AreaID == info.AreaID {
		return
	}
	if err = global.GVA_DB.Model(&user.UserArea{}).Where("user_id = ?", info.UserID).Updates(map[string]interface{}{"user_name": info.UserName, "phone": info.Phone, "card": info.Card, "area_id": info.AreaID}).Error; err != nil {
		log.Println("更新用户信息失败", err)
		return
	}
	return
}

// 	AllAreaUserList 查询所有区县用户信息
func (u *UserSuperService) AllAreaUserList() (userList []response.AreaUserInfoList, err error) {
	list := []common.AreaUser{}
	if err = global.GVA_DB.Table("user_area").Select([]string{"user_id", "user_area.area_id", "user_name", "card", "phone", "name"}).
		Joins("inner join area on area.area_id = user_area.area_id").Find(&list, "is_use = ?", true).Error; err != nil {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	userList = make([]response.AreaUserInfoList, len(list))
	for i, v := range list {
		ur := response.AreaUserInfoList{
			AreaID:   v.AreaID,
			UserID:   v.UserID,
			UserName: v.UserName,
			AreaName: v.Name,
			Card:     v.Card,
			Phone:    v.Phone,
		}
		userList[i] = ur
	}
	return userList, nil
}

// SingleUser 创建单角色用户
func (u *UserSuperService) SingleUser(userInfo request.UserInfo) (err error) {
	if userInfo.Auth[0] == global.Super {
		err = u.CreateSuper(userInfo)
	} else if userInfo.Auth[0] == global.AreaUser {
		err = u.CreateArea(userInfo)
	}

	if err != nil {
		global.GVA_LOG.Error("创建失败", zap.Error(err))
		return err
	}
	return nil
}

// MoreUsers 多角色
func (u *UserSuperService) MoreUsers(userInfo request.UserInfo) (err error) {
	err = u.CreateSuper(userInfo)
	err = u.CreateArea(userInfo)
	if err != nil {
		global.GVA_LOG.Error("创建失败", zap.Error(err))
		return err
	}
	return nil
}

// CreateSuper 创建超级管理员
func (u *UserSuperService) CreateSuper(userInfo request.UserInfo) (err error) {

	// 创建超级管理员都对象
	ur := user.UserSuper{
		UserCommon: global.UserCommon{
			UserName: userInfo.UserName,
			Card:     userInfo.Card,
			Phone:    userInfo.Phone,
			Password: utils.BcryptHash(userInfo.Password),
			AuthID:   global.SuperAuth,
			IsUse:    userInfo.IsUse,
		},
		SuperID: utils.GenID(),
		AreaID:  userInfo.AreaID,
	}
	// 插入到数据表中
	if err = global.GVA_DB.Create(&ur).Error; err != nil {
		return err
	}
	return nil
}

// CreateArea 创建市区用户
func (u *UserSuperService) CreateArea(userInfo request.UserInfo) (err error) {
	// 创建区县用户都对象
	ur := user.UserArea{
		UserCommon: global.UserCommon{
			UserName: userInfo.UserName,
			Card:     userInfo.Card,
			Phone:    userInfo.Phone,
			Password: utils.BcryptHash(userInfo.Password),
			AuthID:   global.AreaAuth,
			IsUse:    userInfo.IsUse,
		},
		UserID: utils.GenID(),
		AreaID: userInfo.AreaID,
	}
	// 插入到数据表中
	if err = global.GVA_DB.Create(&ur).Error; err != nil {
		return err
	}
	return nil
}

// UserEmails 获取所有超级用户的邮箱
func (u *UserSuperService) UserEmails() (email []string, err error) {
	err = global.GVA_DB.Model(&user.UserSuper{}).Select("email").Find(&email).Error
	return
}
